<?php
/*
 * 增加心动值消费，暂时不执行
 */

namespace App\Jobs\QiPao;

use App\Jobs\Job;
use App\Exceptions\BasicException;
use App\Models\MainDB\Blacklist;
use App\Models\MainDB\Group\GroupFriendCard;
use App\Models\MainDB\Group\GroupUsrMatchRecord;
use App\Models\MainDB\RoomPit;
use App\Models\SociatyDB\SociatyUser;
use App\Services\Tools\RedisService;
use App\Services\Tools\RongCloudService;
use Illuminate\Support\Facades\Log;

class ForbidChatJob
{

    protected $data;
    
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }
    
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        return;
        try {
            $fromUserId = str_replace('user_', '', $this->data['fromUserId']);
            $toUserId   = str_replace('user_', '', $this->data['toUserId']);
            $chatRes    = $this->checkCanSendMsg($fromUserId, $toUserId);

            if (!$chatRes) {
                file_put_contents(
                    storage_path() . '/logs/chatMessage-' . date('Ymd') . '.log',
                    '私聊信息' . json_encode($this->data) . PHP_EOL, FILE_APPEND
                );
                (new RongCloudService())->blockUser($this->data['fromUserId'], 43200);
            }
        } catch (BasicException $exception) {
            echo $exception->getMessage();
        } catch (\Exception $exception) {
            Log::error(__CLASS__.':'.$exception->getMessage());
        }
    }


    //校验是否可以发送私聊
    public function checkCanSendMsg($user_id, $user_id_to)
    {
        $pit = RoomPit::query()->where(['user_id' => $user_id])->first();
        if (!empty($pit)) {
            return true;
        }

        //速配和恋爱专线判断
        $matchRecord = GroupUsrMatchRecord::query()
            ->where("user_id", $user_id)
            ->where("match_user_id", $user_id_to)
            ->where(['status' => 0])
            ->select('id')->get();
        if (!$matchRecord->isEmpty()) {
            return true;
        }
        //恋爱专线
        $friendCardLockRecord = GroupFriendCard::query()
            ->join('ln_group_friend_card_unlock_log as cu', function ($join) use ($user_id) {
                $join->on('cu.card_id', '=', 'ln_group_friend_card.id');
                $join->on('ln_group_friend_card.gift_id', '=', 'cu.gift_id');
            })
            ->whereRaw("(ln_group_friend_card.user_id = '" . $user_id_to . "' and cu.user_id = '" . $user_id . "') or (ln_group_friend_card.user_id = '" . $user_id . "' and cu.user_id = '" . $user_id_to . "')")
            ->where(['ln_group_friend_card.is_delete' => 0])
            ->select('cu.id')->first();
        if (!empty($friendCardLockRecord)) {
            return true;
        }

        // 验证是否拉黑
        $condition  = "(user_id = '$user_id' and black_id = '$user_id_to') or (user_id = '$user_id_to' and black_id = '$user_id')";
        $blackUsers = Blacklist::query()->whereRaw($condition)->get();
        if (!$blackUsers->isEmpty()) {
            return false;
        }

        //校验权限
        $is_guild_user    = RedisService::checkGuildUser($user_id); //A是否公会成员
        $is_friend        = RedisService::checkFriend($user_id, $user_id_to); //AB是否好友
        $is_guild_user_to = RedisService::checkGuildUser($user_id_to);//B是否公会成员
        if ($is_friend) { //是好友可以发送私聊
            return true;
        }
//
//        2022-0803 产品要求不是好友也能聊天
        if (!$is_guild_user && !$is_guild_user_to) {  //都不是公会成员
            return false;
        }

        //$where = ['user_id' => [[$user_id_to,$user_id], 'IN']];
        // 不确定一个用户是否会加入多个用户
        $sendUserSpeakStatus   = RedisService::checkIsAllowChat($user_id);
        $targetUserSpeakStatus = RedisService::checkIsAllowChat($user_id_to);
        $guildUserIsSpeak      = $sendUserSpeakStatus ? 2 : 1; //1不允许 2允许
        $guildUserToIsSpeak    = $targetUserSpeakStatus ? 2 : 1; //1不允许 2允许
        $isSpeak               = SociatyUser::query()->whereIn('user_id', [$user_id_to, $user_id])->select(['user_id','is_speak'])->get();
        foreach ($isSpeak as $k => $v) {
            if ($v['user_id'] == $user_id) {
                $guildUserIsSpeak = $v->is_speak;
            }
            if ($v['user_id'] == $user_id_to) {
                $guildUserToIsSpeak = $v->is_speak;
            }
        }

        if ($is_guild_user && $is_guild_user_to) {    //20210629私聊新增 都是公会成员只要一方开启私聊权限即可
            if ($guildUserIsSpeak == 2 || $guildUserToIsSpeak == 2) {
                return true;
            }
        }

        $isOk = false;
        $msg  = '成为好友才能发送消息';
        if ($is_guild_user) {
            $isOk = true;
        } else {
            if ($is_guild_user_to) {
                if ($guildUserToIsSpeak == 2) {
                    $isOk = true;
                } else {
                    $isOk = false;
                }
            }
        }
        if (!$isOk) {
            return false;
        } else {
            return true;
        }
    }

}